package org.kumas.bigdata.hdfs3.fsdirectory;

/*************************************************
 * TODO_Kumas
 *  Author： KumasZhang
 *  DateTime： 2021-12-02 15:28
 *  Description： 
 **/
public class INodeFile extends INode {

    private Block[] blocks;

    public INodeFile(String fileName, INodeDirectory parent, Block[] blocks) {
        super(fileName, parent);
        setBlocks(blocks);
    }

    private void setBlocks(Block[] blocks) {
        this.blocks = (blocks != null ? blocks : Block.EMPTY_ARRAY);
    }

    public void addBlock(Block newblock) {
        if (this.blocks.length == 0) {
            this.setBlocks(new Block[]{newblock});
        } else {
            int size = this.blocks.length;
            Block[] newlist = new Block[size + 1];
            System.arraycopy(this.blocks, 0, newlist, 0, size);
            newlist[size] = newblock;
            this.setBlocks(newlist);
        }
    }

    @Override
    public final boolean isFile() {
        return true;
    }

    public final INodeFile asFile() {
        return this;
    }

    public Block[] getBlocks() {
        return this.blocks;
    }

}
